home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / P Examples / date.p < prev    next >
Encoding:
Text File  |  1994-08-28  |  2.7 KB  |  124 lines  |  [TEXT/PJMM]

  1. unit aCommand;
  2.  
  3. {  ================================================== }
  4.  
  5. { date.p - an examle of a simple nShell (tm) command. }
  6.  
  7. { Copyright ( c ) 1994 Newport Software Development }
  8.  
  9. { You may distribute unmodified copies of this file for noncommercial }
  10. { purposes . You may use this file as a reference when writing your }
  11. { own nShell ( tm ) commands . }
  12.  
  13. { All other rights are reserved . }
  14.  
  15. {  ================================================== }
  16.  
  17. interface
  18.  
  19. { The NSHC unit defines the interfaces nshell to callbacks.  See "nshc.p" }
  20.  
  21.     uses
  22.         NSHC;
  23.  
  24. { The routine "theCommand" is called by the nShell.lib to to the work of the command. }
  25.  
  26.     procedure theCommand (nshc_parms: t_nshc_parms; nshc_calls: t_nshc_calls);
  27.  
  28. {  ================================================== }
  29.  
  30. implementation
  31.  
  32. {  ================================================== }
  33.  
  34. { utility commands }
  35.  
  36.     function got_option (nshc_parms: t_nshc_parms; option: char): boolean;
  37.         var
  38.             found: boolean;
  39.             arg, argc: integer;
  40.             i: integer;
  41.             c: char;
  42.     begin
  43.         found := false;
  44.  
  45.         arg := 1;
  46.         argc := nshc_parms^.argc;
  47.  
  48.         while not found and (arg < argc) do
  49.             begin
  50.                 i := nshc_parms^.argv[arg];
  51.                 c := nshc_parms^.arg_buf[i];
  52.                 if c = '-' then
  53.                     while (c <> chr(0)) and not found do
  54.                         begin
  55.                             i := succ(i);
  56.                             c := nshc_parms^.arg_buf[i];
  57.                             found := c = option;
  58.                         end;
  59.                 arg := succ(arg);
  60.             end;
  61.  
  62.         got_option := found;
  63.  
  64.     end;
  65.  
  66. {  ================================================== }
  67.  
  68. { display the date in the specified format }
  69.  
  70.     procedure DoDate (nshc_parms: t_nshc_parms; nshc_calls: t_nshc_calls);
  71.         var
  72.             seconds: Boolean;
  73.             secs: LONGINT;
  74.             s: Str255;
  75.             date_fmt: DateForm;
  76.             i: integer;
  77.     begin
  78.         if (got_option(nshc_parms, 's')) then
  79.             date_fmt := shortDate
  80.         else if (got_option(nshc_parms, 'a')) then
  81.             date_fmt := abbrevDate
  82.         else
  83.             date_fmt := longDate;
  84.  
  85.         GetDateTime(secs);
  86.         IUDateString(secs, date_fmt, s);
  87.         NSH_putStr(nshc_calls, s);
  88.  
  89.         if (not got_option(nshc_parms, 'd')) then
  90.             begin
  91.                 NSH_putchar(nshc_calls, ' ');
  92.                 seconds := not got_option(nshc_parms, 'm');
  93.                 IUTimeString(secs, seconds, s);
  94.                 NSH_putStr(nshc_calls, s);
  95.             end;
  96.  
  97.         NSH_putchar(nshc_calls, RETURN_CHAR);
  98.     end;
  99.  
  100. {  ================================================== }
  101.  
  102. { pascal 'main' for commands }
  103.  
  104.     procedure theCommand (nshc_parms: t_nshc_parms; nshc_calls: t_nshc_calls);
  105.         var
  106.             s: Str255;
  107.     begin
  108.  
  109.         nshc_parms^.action := nsh_idle;
  110.         nshc_parms^.result := 0;
  111.  
  112.         if nshc_parms^.version <> nshc_version then
  113.             begin
  114.                 s := 'This command is not of a compatible version.';
  115.                 NSH_putStr_err(nshc_calls, s);
  116.                 NSH_putchar_err(nshc_calls, RETURN_CHAR);
  117.                 nshc_parms^.result := NSHC_ERR_VERSION;
  118.             end
  119.         else
  120.             DoDate(nshc_parms, nshc_calls);
  121.  
  122.     end;
  123.  
  124. end.